<template>
  <div>
    <scene-seckill-dialog v-if="info"
      :info="info"
      :windowName="windowName"
      isPage
      ref="seckillRef"/>
  </div>
</template>

<script>
import sceneSeckillDialog from '@/components/diy-dialog/components/scene-seckill-dialog.vue'
import { getPrdoductDetail } from '@/api/common'
export default {
  name: 'SceneSeckill',
  components: {
    sceneSeckillDialog
  },
  data () {
    return {
      info: null,
      windowName: 'XZDesktopFlashSale'
    }
  },
  created (ops) {
    this.$xzReg('inform_self_param', res => {
      this.toPageForParam(res)
    })
    this.$xz('xz_set_window_size', this.windowName, 680, 440)
  },
  mounted () {
    this.$nextTick(() => {
      this.$xz('xz_page_load_finished', this.windowName)
    })
  },
  methods: {
    toPageForParam (obj) {
      let data
      try {
        data = JSON.parse(obj)
      } catch (e) {
        if (obj) {
          obj = decodeURIComponent(obj)
          // 解决跨端通讯，字符串存在 / 被客户端截断问题
          try {
            data = JSON.parse(obj)
          } catch (e) {
            data = {}
          }
        } else {
          data = {}
        }
      }
      if (data.toWinId === this.windowName) {
        this.info = data
        this.init()
      }
    },
    async init () {
      if (this.hasLogin) {
        await this.$store.dispatch('user/getUserInfo')
      }

      await this.getData()
    },
    async getData () {
      if (typeof this.info.product === 'undefined' && this.info.detail?.product_ids?.length) {
        // 获取商品
        try {
          const res = await getPrdoductDetail({ id: this.info.detail.product_ids[0] })
          this.$set(this.info, 'product', res.data)
        } catch (error) {}
      }
    }
  }
}
</script>
